package com.xty.xtyle.base;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.xty.xtyle.R;
import com.xty.xtyle.db.cloth.Cloth;
import com.xty.xtyle.db.cloth.ClothDao;
import com.xty.xtyle.db.mix.MixCloth;
import com.xty.xtyle.db.mix.MixDao;
import com.xty.xtyle.db.mix.MixTypeDao;
import com.xty.xtyle.detail.ClothDetailActivity;
import com.xty.xtyle.event.FreshMixEvent;
import com.xty.xtyle.mix.MixDetialActivity;
import com.xty.xtyle.utils.CommonUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

/**
 * 混搭tab页面
 */
public class MixTabFragment extends AbsFragment {
    private RecyclerView recyclerView;
    private View cloth_empty;
    List<MixCloth> cloths;
    ClothDao clothDao = new ClothDao();

    @Override
    public int getLayoutId() {
        return R.layout.fragment_tab;
    }

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.rv);
        cloth_empty = findViewById(R.id.cloth_empty);
       iii();

        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);

    }


    void iii() {
        Bundle bundle = getArguments();
        String type = bundle.getString("type");

        MixDao dao = new MixDao();
        cloths = dao.queryAllByType(type);
        if (null == cloths || cloths.isEmpty()) {
            showEmpty(true);
        }else {
            showEmpty(false);
            recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
            recyclerView.setAdapter(new StringAdapter());
        }
    }

    @Subscribe
    public void onEvent(FreshMixEvent result) {
        //接收以及处理数据
        iii();
    };


    @Override
    public void freshData() {}

    private void showEmpty(boolean show) {
        if (show) {
            cloth_empty.setVisibility(View.VISIBLE);
        }else {
            cloth_empty.setVisibility(View.GONE);
        }
    }

    private class StringAdapter extends RecyclerView.Adapter<StringAdapter.VH> {

        @NonNull
        @Override
        public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new VH(LayoutInflater.from(parent.getContext()).inflate( R.layout.item_mix, parent, false));
        }

        /**
         * 这里混搭只显示衣服和裤子,配件没有显示
         */
        @Override
        public void onBindViewHolder(@NonNull VH holder, int position) {
            final MixCloth mixCloth = cloths.get(position);
            final long id = mixCloth.getId();
            Cloth cloth1 = clothDao.findCloth(mixCloth.getUpId());
            if (null != cloth1)
                Glide.with(holder.itemView).load(cloth1.getImage()).into(holder.ivCover);
            Cloth cloth2 = clothDao.findCloth(mixCloth.getDownId());
            if (null != cloth2)
                Glide.with(holder.itemView).load(cloth2.getImage()).into(holder.ivDown);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MixDetialActivity.startActivity(view.getContext(), id);
                }
            });
        }

        @Override
        public int getItemCount() {
            return cloths.size();
        }

        class VH extends RecyclerView.ViewHolder {
            ImageView ivDown;
            ImageView ivCover;
            public VH(@NonNull View itemView) {
                super(itemView);
                ivDown = itemView.findViewById(R.id.iv_down);
                ivCover = itemView.findViewById(R.id.iv_cover);
            }
        }
    }

}
